<?php
/**
 * Blog archives
 */

$loggedin_user = elgg_get_logged_in_user_entity();
$page_owner = elgg_get_page_owner_entity();
//print_r($page_owner);

if (elgg_instanceof($page_owner, 'user')) {
	$url_segment = 'blog/archive/' . $page_owner->username;
} elseif ($vars['page']=='all'||$vars['page']=='category'||$vars['page']=='archive') {
	$url_segment = 'blog/archive/all';
} else {
	$url_segment = 'blog/group/' . $page_owner->getGUID() . '/archive';
}

// This is a limitation of the URL schema.
if (($vars['page']=='all'||$vars['page']=='category'||$vars['page']=='archive')||($page_owner && $vars['page'] != 'friends')) {
	if ($vars['page']=='all'||$vars['page']=='category'||$vars['page']=='archive') {
		$dates = get_entity_dates('object', 'blog', 0, 0, 'time_created desc');
	} else {
	$dates = get_entity_dates('object', 'blog', $page_owner->getGUID(), 0, 'time_created desc');
	}

	if ($dates) {
		$title = elgg_echo('blog:archives');
		//$content = '<ul class="blog-archives" style="max-height:200px;">';
		$content = '<table>';
		$i = 0;
		foreach ($dates as $date) {
			$timestamplow = mktime(0, 0, 0, substr($date,4,2) , 1, substr($date, 0, 4));
			$timestamphigh = mktime(0, 0, 0, ((int) substr($date, 4, 2)) + 1, 1, substr($date, 0, 4));

			$link = elgg_get_site_url() . $url_segment . '/' . $timestamplow . '/' . $timestamphigh;
			//$month = elgg_echo('date:month:' . substr($date, 4, 2), array(substr($date, 0, 4)));
			$month = elgg_echo('date:mm:' . substr($date, 4, 2), array(substr($date, 0, 4)));
			if ($i%2==0) $content .= '<tr>';
			$content .= "<td width='80px'><a href=\"$link\" title=\"$month\" style='font-family:consolas'>$month</a></td>";
			//$content .= "<li><a href=\"$link\" title=\"$month\">$month</a></li>";
                        //$content .= "<a href=\"$link\" title=\"$month\">$month</a>";
			if ($i%2==1) $content .= '<tr>';
			$i += 1;
		}
		$content .= '</table>';
		//$content .= '</ul>';

		echo elgg_view_module('aside', $title, $content);
	}
}
